home *** CD-ROM | disk | FTP | other *** search
- Path: iz.maus.de!Torsten_Landschoff
- From: Torsten_Landschoff@iz.maus.de (Torsten Landschoff)
- Newsgroups: comp.lang.c++
- Subject: character strings read-in problem!
- Message-ID: <199603040716.a35477@iz.maus.de>
- Date: Mon, 04 Mar 96 05:16:00 GMT
- References: <4h2mkr$5au@sunburst.ccs.yorku.ca>
- X-Gate: MausGate/News 1.25/ac3
- MIME-Version: 1.0
- Content-Type: text/plain; charset=ISO-8859-1
- Content-Transfer-Encoding: 8bit
-
- -A13634@AC3
-
- Hi KuMan
-
- K>I'm trying to read in strings from a file and put them in an array of
- K>26 different cells according to their order alphabetically.
-
- K>#include <stdio.h>
- K>#include <stdlib.h>
-
- K>void main(){
-
- better: int main( void ) {
-
- K>FILE *input;
- K>char name[20];
- K>char *stop_word[26];
-
- K>input = fopen("QQ.txt", "r");
- K>while(!feof(input)){
- K> fscanf(input, "%s", name);
- K> stop_word[((name[0] >= 97)? name[0]-96 : name[0]%64)] = name;
-
- Here is the problem: You can't assign a character string as any ordinary type -
- you must call a special function to do that. After this assignment every assign
- entry of stop_word will contain a pointer to name, which has the value of the
- last string read.
-
- To put this right, you could replace the original line by the following:
-
- stop_word[((name[0] >= 97) ? name[0]-96 : name[0]%64)] = strdup(name);
-
- K>}
- printf("%s \n", stop_word[1]);
- printf("%s \n", stop_word[3]);
- }
- fclose(input);
- }
-
- cu
- Torsten
-